package com.lungle.warmdiary.app.view.testcampaign.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.model.event.CampaignGoToDetailListRate;
import com.lungle.warmdiary.app.ui.tab.SlidingTabLayout;
import com.lungle.warmdiary.app.view.testcampaign.adapter.CampaignDetailListPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by zhangxueyuan on 16/9/29.
 *
 * 亲测详情右边的“亲测” 列表
 */
public class CampaignDetailListPagerFragment extends Fragment {
    private ViewPager mViewPager;
    private CampaignDetailListPagerAdapter mTabAdapter;
    private SlidingTabLayout mSlidingTabLayout;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.v2_fragment_viewpager2, container,
                false);

        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.tab_indicator, R.id.tv_media_name);
        Resources res = getResources();
        int color = res.getColor(R.color.main_red);
        mSlidingTabLayout.setSelectedIndicatorColors(color);
        mSlidingTabLayout.setDistributeEvenly(true);

        mViewPager = (ViewPager) view.findViewById(R.id.main_tab_pager);
        mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.view_pager_margin));


        if (mTabAdapter == null) {
            mTabAdapter = new CampaignDetailListPagerAdapter(getChildFragmentManager());
        }
        mViewPager.setOffscreenPageLimit(mTabAdapter.getCount());
        mViewPager.setAdapter(mTabAdapter);

        mSlidingTabLayout.setViewPager(mViewPager);

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void gotoListRate(CampaignGoToDetailListRate event) {
        mViewPager.setCurrentItem(event.getId());
    }
}
